package avicit.bdp.dms.tdm.rest;

import avicit.bdp.common.database.ColumnMeta;
import avicit.bdp.dms.das.dto.BdpDasColumnViewSettingDTO;
import avicit.bdp.dms.das.service.BdpDasColumnViewSettingService;
import avicit.bdp.dms.tdm.dto.BdpAssetDTO;
import avicit.bdp.dms.tdm.dto.BdpAssetMountModelDTO;
import avicit.bdp.dms.tdm.service.*;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：${tableInfo.classCreator}
 * @邮箱：${tableInfo.classMailbox}
 * @创建时间： 2023-11-14 16:04
 * @类说明：bdp_asset_portalRest
 * @修改记录：
 */
@RestController
@Api(tags = "数据资产门户")
@RequestMapping("/api/bdp/dms/asset/portal")
public class AssetPortalRest {

    private static final Logger logger = LoggerFactory.getLogger(AssetPortalRest.class);

    @Autowired
    private AssetPortalService assetPortalService;
    @Autowired
    private BdpDasColumnViewSettingService columnViewSettingService;
    @Autowired
    private AssetLogService assetLogService;
    @Autowired
    private BdpAssetDownloadService assetDownloadService;
    @Autowired
    private BdpAssetService assetService;
    @Autowired
    private BdpAssetMountModelService bdpAssetMountModelService;

    @ApiOperation(value = "分页查询已发布资产")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "目录ID", name = "catalogId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "资源类型", name = "assetType", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "标签", name = "labels", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "发布方", name = "releaseDept", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "数据来源", name = "sourceSystem", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "预览量排序", name = "viewSort", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
    })
    @GetMapping("/getAssetPageList")
    public ResponseMsg<QueryRespBean<BdpAssetDTO>> getAssetPageList(
            @RequestParam(required = false) String catalogId,
            @RequestParam(required = false) String keyWords,
            @RequestParam(required = false) String assetType,
            @RequestParam(required = false) String labels,
            @RequestParam(required = false) String releaseDept,
            @RequestParam(required = false) String sourceSystem,
            @RequestParam(required = false) String viewSort,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        ResponseMsg<QueryRespBean<BdpAssetDTO>> responseMsg = new ResponseMsg<>();
        QueryRespBean<BdpAssetDTO> queryRespBean = assetPortalService.getAssetPageList(catalogId, keyWords, assetType, labels, releaseDept, sourceSystem, viewSort, pageNo, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    @ApiOperation(value = "分页查询资产监控列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "目录ID", name = "catalogId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "资源类型", name = "assetType", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "发布方", name = "releaseDept", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "发布日期开始", name = "releaseDateBegin", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "发布日期结束", name = "releaseDateEnd", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
    })
    @GetMapping("/getAssetMonitorPageList")
    public ResponseMsg<QueryRespBean<BdpAssetDTO>> getAssetMonitorPageList(
            @RequestParam(required = false) String catalogId,
            @RequestParam(required = false) String keyWords,
            @RequestParam(required = false) String assetType,
            @RequestParam(required = false) String releaseDept,
            @RequestParam(required = false) String releaseDateBegin,
            @RequestParam(required = false) String releaseDateEnd,
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        ResponseMsg<QueryRespBean<BdpAssetDTO>> responseMsg = new ResponseMsg<>();
        QueryRespBean<BdpAssetDTO> queryRespBean = assetPortalService.getAssetMonitorPageList(catalogId, keyWords, assetType, releaseDept,
                releaseDateBegin, releaseDateEnd, pageNo, pageSize);
        responseMsg.setResponseBody(queryRespBean);
        return responseMsg;
    }

    @ApiOperation(value = "查询资产信息")
    @ApiParam(value = "id", name = "id", required = true)
    @GetMapping("/getAssetById")
    public ResponseMsg<BdpAssetDTO> getAssetById(@RequestParam String id) {
        ResponseMsg<BdpAssetDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetPortalService.getAssetById(id));
        return responseMsg;
    }


    @ApiOperation(value = "数据查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String"),
            @ApiImplicitParam(value = "参数josn", name = "paramJson", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNum", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
    })
    @PostMapping("/queryTableData")
    public ResponseMsg<Map<String, Object>> queryTableData(
            @ApiIgnore @RequestBody Map<String, Object> queryMap) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetPortalService.queryTableDataNew(queryMap));
        return responseMsg;
    }

    @ApiOperation(value = "模型查看")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String")
    })
    @GetMapping("/queryModel")
    public ResponseMsg<List<ColumnMeta>> queryModel(@RequestParam String assetId) throws Exception {
        ResponseMsg<List<ColumnMeta>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetPortalService.queryModel(assetId));
        return responseMsg;
    }

    @ApiOperation(value = "文件下载")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String"),
            @ApiImplicitParam(value = "文件路径", name = "filePath", dataType = "String")
    })
    @GetMapping("/downloadFile")
    public void downloadFile(@RequestParam String assetId, @RequestParam String filePath,
                             HttpServletRequest request,  HttpServletResponse response) throws Exception {
        assetPortalService.downloadFile(assetId, filePath, request, response);
    }

    @ApiOperation(value = "API查看")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String")
    })
    @GetMapping("/queryApi")
    public ResponseMsg<Map<String, Object>> queryApi(@RequestParam String assetId) throws Exception {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetPortalService.queryApi(assetId));
        return responseMsg;
    }

    @ApiOperation(value = "报表查看")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String")
    })
    @GetMapping("/queryReport")
    public ResponseMsg<String> queryReport(@RequestParam String assetId) throws Exception {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetPortalService.queryReport(assetId));
        return responseMsg;
    }

    @ApiOperation(value = "数据下载")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String")
    })
    @GetMapping("/download")
    public void download(@RequestParam String assetId, HttpServletResponse response)
            throws Exception {
        assetPortalService.download(assetId, response);
    }

    /**
     * 数据查看设置字段属性
     */
    @ApiOperation(value = "保存表数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "表模型ID", name = "tableId", dataType = "String"),
            @ApiImplicitParam(value = "参数josn", name = "paramJson", dataType = "String")
    })
    @PostMapping(value = "/saveColumnSetting")
    public ResponseMsg<String> saveColumnSetting(
            @ApiIgnore @RequestBody BdpDasColumnViewSettingDTO viewSettingDTO) {

        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(columnViewSettingService.saveColumnSetting(viewSettingDTO));
        return responseMsg;
    }

    @GetMapping(value = "/getColumnSetting")
    @ApiOperation(value = "查询表数据列表")
    @ApiImplicitParam(value = "表模型ID", name = "tableId", dataType = "String")
    public ResponseMsg<BdpDasColumnViewSettingDTO> getColumnSetting(@RequestParam(value = "tableId") String tableId) {

        ResponseMsg<BdpDasColumnViewSettingDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(columnViewSettingService.getColumnSetting(tableId));
        return responseMsg;
    }

    @ApiOperation(value = "更新查看次数")
    @PostMapping("/addViewCount")
    public ResponseMsg<String> addViewCount(@RequestBody BdpAssetDTO asset) throws Exception {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        assetService.addViewCount(asset);
        return responseMsg;
    }

    @ApiOperation(value = "报表增加服务次数")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String")
    })
    @GetMapping("/reportUrlLog")
    public ResponseMsg<String> reportUrlLog(@RequestParam String assetId) throws Exception {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        assetLogService.reportUrlLog(assetId);
        return responseMsg;
    }

    @ApiOperation(value = "查询下载文件状态")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String")
    })
    @GetMapping("/checkDataDownload")
    public ResponseMsg<Map<String,Object>> checkDataDownload(@RequestParam String assetId) {
        ResponseMsg<Map<String,Object>> responseMsg = new ResponseMsg<>();
        Map<String,Object> map = assetDownloadService.checkDataDownloadById(assetId);
        responseMsg.setResponseBody(map);
        return responseMsg;
    }

    @ApiOperation(value = "生成临时文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "assetId", name = "assetId", dataType = "String")
    })
    @GetMapping("/generateTempFile")
    public ResponseMsg<String> generateTempFile(@RequestParam String assetId) throws IOException {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetDownloadService.generateTempFile(assetId));
        return responseMsg;
    }

    @ApiOperation(value = "下载临时文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "fileId", name = "fileId", dataType = "String")
    })
    @GetMapping("/downloadTempFile")
    public void downloadTempFile(@RequestParam String fileId, HttpServletResponse response) throws Exception {
        assetDownloadService.downloadTempFile(fileId,response);
    }

    @ApiOperation(value = "首页概览")
    @GetMapping("/homeOverview")
    public ResponseMsg<Map<String, Object>> overview() throws Exception {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetLogService.homeOverview());
        return responseMsg;
    }

    @ApiOperation(value = "查询资产目录")
    @GetMapping("/homeCategorys")
    public ResponseMsg<List<Map<String,String>>> homeCategorys() throws Exception {
        ResponseMsg<List<Map<String,String>>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetLogService.homeCategorys());
        return responseMsg;
    }

    @ApiOperation(value = "查询引入数据源")
    @GetMapping("/getAssetDatasources")
    public ResponseMsg<List<Map<String,Object>>> getAssetDatasources() throws Exception {
        ResponseMsg<List<Map<String,Object>>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetLogService.getAssetDatasources());
        return responseMsg;
    }

    @ApiOperation(value = "通过资产ID查询模型")
    @ApiParam(value = "assetId", name = "assetId", required = true)
    @GetMapping("/queryModelByAssetId")
    public ResponseMsg<BdpAssetMountModelDTO> queryModelByAssetId(@RequestParam String assetId) {
        ResponseMsg<BdpAssetMountModelDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(bdpAssetMountModelService.queryByAssetId(assetId));
        return responseMsg;
    }

    @ApiOperation(value = "我的申请详情查询申请按钮状态")
    @ApiParam(value = "assetId", name = "assetId", required = true)
    @GetMapping("/queryApplyByAssetId")
    public ResponseMsg<Boolean> queryApplyByAssetId(@RequestParam String assetId) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(assetPortalService.queryApplyByAssetId(assetId));
        return responseMsg;
    }

}